package org.lonetree.tc.webapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Course;
import org.lonetree.tc.core.Course.CourseId;
import org.lonetree.tc.core.dao.CourseDAO;
import org.lonetree.tc.core.dao.DAOFactory;

public class ViewCourseDetails extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		try{
			CourseId cid=new CourseId(request.getParameter("courseId"));
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			CourseDAO cdao = factory.getCourseDAO();
			Course course=cdao.getCourse(cid);
			
			request.setAttribute("course", course);
			request.setAttribute("comments", course.getComments().iterator());
			request.setAttribute("sections", course.getSections().iterator());	
			
			return mapping.findForward("success");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return mapping.findForward("failure");
		}
			
	}
	
}